package cn.edu.gdmec.android.beatbox;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.List;

/**
 * Created by Jack on 2017/11/19.
 */

public class BeatBoxFragment extends Fragment{

    //创建实例
    private BeatBox mBeatBox;

    public static BeatBoxFragment newInstance(){
        return new BeatBoxFragment ();
    }
    //实例化
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate ( savedInstanceState );
        //调用
        setRetainInstance(true);
        mBeatBox = new BeatBox ( getActivity () );
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        View view = inflater.inflate ( R.layout.fragment_beat_box, container, false );

        RecyclerView recyclerView = (RecyclerView)view
                .findViewById ( R.id.fragment_beat_box_recycler_view );
        recyclerView.setLayoutManager ( new GridLayoutManager ( getActivity (), 3 ) );
        //recyclerView.setAdapter ( new SoundAdapter (  ) );
        //设置adapter
        recyclerView.setAdapter ( new SoundAdapter ( mBeatBox.getSounds () ) );

        return view;
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        mBeatBox.release();
    }

    //添加点击按钮播放
    private class SoundHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private Button mButton;
        //绑定sound
        private Sound mSound;

        public SoundHolder(LayoutInflater inflater, ViewGroup container){
            super(inflater.inflate ( R.layout.list_item_sound, container, false ));

            mButton = (Button)itemView.findViewById ( R.id.list_item_sound_button );
            //添加点击按钮播放
            mButton.setOnClickListener ( this );
        }
        //绑定sound
        public void bindSound(Sound sound){
            mSound = sound;
            mButton.setText(mSound.getName());
        }
        //添加点击按钮播放
        @Override
        public void onClick(View v){
            mBeatBox.play ( mSound );
        }
    }


    private class SoundAdapter extends RecyclerView.Adapter<SoundHolder>{
        //绑定sound列表
        private List<Sound> mSounds;
        public SoundAdapter(List<Sound> sounds){
            mSounds = sounds;
        }

        @Override
        public SoundHolder onCreateViewHolder(ViewGroup parent, int viewType){
            LayoutInflater inflater = LayoutInflater.from ( getActivity () );
            return new SoundHolder ( inflater, parent );
        }
        @Override
        public void onBindViewHolder(SoundHolder soundHolder, int position){
            //绑定sound列表
            Sound sound = mSounds.get ( position );
            soundHolder.bindSound(sound);

        }
        @Override
        public int getItemCount(){
            //return 0;
            return mSounds.size ();
        }
    }
}
